<template>
  <view class="content">
    <button class="btn1" @click="handlerAuth">
      <text>一键登录</text>
    </button>
    <view @tap.stop="isGx=!isGx" class="xy">
      <view class="iconfont icon-selected-copy icon" v-if="isGx" ></view>
      <view class="iconfont icon-weixuanzhong icon" v-else ></view>
      <text class="text1">我已阅读并同意辉鲨司机</text>
      <text class="text2" @tap.stop="goNext('/pages/xy/xy?key=yhxy')">《用户协议》</text>
      <text class="text2" @tap.stop="goNext('/pages/xy/xy?key=yszc')">《隐私政策》</text>
    </view>

  </view>
</template>

<script>
import {getOpenid} from "../../common/login";

export default {
  data() {
    return {
      isGx: false,//是否勾选同意
    }
  },
  onLoad() {
    /*  request({
        url:'/d/user/getOpenid'
      })*/
  },
  methods: {
    handlerAuth() {
      if (!this.isGx) return uni.showToast({
        title:'请阅读并同意相关协议',
        icon:'none'
      })
      const callBack = "https://huishahuoyun.cjzxkj.com/h5"
      window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + 'wx21ad6e7757368fc0' + '&redirect_uri=' +
          encodeURIComponent(callBack) + '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
    },

  //跳转
    goNext(url) {
      uni.navigateTo({
        url: url
      })
    },
    //获取手机号
    async getPhoneNumber(e) {
      let that = this
      if (!this.isGx) {
        uni.showToast({
          icon: "none",
          title: "请阅读并同意用户协议和隐私政策"
        })
        return
      }
      let openid = uni.getStorageSync('openid')
      if (!openid) {
        uni.showToast({
          icon: "none",
          title: "openid不能为空"
        })
        return
      }
      if (e.detail.errMsg != 'getPhoneNumber:ok') {
        uni.showToast({
          title: '请允许获取您的手机号',
          icon: 'none',
        })
        return
      }

      // let data = await this.$request({
      // 		url: '/d/user/getPhoneNumber',
      // 		method: 'POST',
      // 		isAuth: false,
      // 		data: {
      // 			code: e.detail.code
      // 		}
      // });
      //data.data.phoneNumber
      that.wxlogin(openid, '15025731797')
    },
    //微信授权
    async wxlogin(openid, mobile) {
      let data = await this.$request({
        url: '/user/wxlogin',
        method: 'POST',
        isAuth: false,
        data: {
          pid: 0,
          openid: openid,
          avatar: '',
          nickname: '',
          mobile: mobile
        }
      });
      //缓存token
      uni.setStorageSync('token', data.data.userinfo.token)
      //缓存用户信息
      uni.setStorageSync('user', data.data.userinfo)
      // 跳转回去
      let pages = getCurrentPages()
      // 没有上级页面，直接返回首页
      if (pages.length == 1) {
        uni.reLaunch({
          url: '/pages/index/index'
        })
      } else {// 有上级 返回
        let lastPageRoute = pages.slice(-2)[0].route; // 获取最近两个页面的路由信息数组，并选择第二个元素（即上次打开的页面）
        // 调用跳转函数，传入目标页面路由信息
        uni.navigateBack({
          delta: 1, // 返回上一级页面
        });
        setTimeout(() => {
          uni.reLaunch({
            url: '/' + lastPageRoute, // 重新加载上次打开的页面
          });
        }, 500); // 等待页面关闭动画完成后再进行跳转
      }
    },

  }
}
</script>

<style lang="scss">
.content {
  text-align: center;

  .btn1 {
    margin: 380rpx 40rpx 0;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background-color: #895FFF;
    color: #fff;
    font-size: $uni-font-size-base;
    font-weight: bold;
    border-radius: 40rpx;
    letter-spacing: 5rpx;
  }

  .xy {
    margin-top: 30rpx;
    line-height: 50rpx;
    font-size: $uni-font-size-sm;
    display: flex;
    justify-content: center;

    .icon {
      font-size: 36rpx;
    }

    .text1 {
      margin-left: 5rpx;
    }

    .text2 {
      color: #895FFF;
    }
  }

}
</style>
